package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"user3/pkg"
)

func JwtAuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("经过了中间件")
		tokenString := c.Request.Header.Get("Authorization")
		if tokenString == "" {
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
				"code": 2000,
				"msg":  "用户认证失败",
			})
			return
		}
		userid, err := pkg.ParseJwtToken(tokenString)
		if err != nil {
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
				"code": 2000,
				"msg":  "token解析失败",
			})
			return
		}

		c.Set("userid", userid)
		fmt.Println("控制器前面")
		c.Next()
		fmt.Println("控制器后面")
	}
}
